package com.yf.dxs.wo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.yf.dxs.wo.consts.WoEventConst;
import com.yf.dxs.wo.dao.WoLevelMsgMapper;
import com.yf.dxs.wo.entity.WoLevelMsgDO;
import com.yf.dxs.wo.redis.RedisMqTemplate;
import com.yf.dxs.wo.req.msg.WoLevelMsg;
import com.yf.dxs.wo.service.MsgHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 注册时候会先通知
 * 成为客户
 * 成为会员
 * 开卡
 */
@Service
@Slf4j
public class MemberCardLevelHandler implements MsgHandler {

    @Resource
    private WoLevelMsgMapper woLevelMsgMapper;

    @Resource
    private RedisMqTemplate redisMqTemplate;


    public void handle(String msgId, String msg, String event) {
        //开卡事件
        if (event.equals(WoEventConst.CHANGE)) {
            WoLevelMsg woLevelMsg = JSON.parseObject(msg, WoLevelMsg.class);
            WoLevelMsgDO entity = woLevelMsg.toEntity();
            entity.setMsgId(msgId);
            log.info("处理级别:{},{},{},{}", woLevelMsg.getWid(), woLevelMsg.getNewLevelId(), woLevelMsg.getOldLevelId(), woLevelMsg.getChangeTime());
            int i = woLevelMsgMapper.insert(entity);
            if (i == 0) {
                log.error("处理失败:{},{},{},{}", woLevelMsg.getWid(), woLevelMsg.getNewLevelId(), woLevelMsg.getOldLevelId(), woLevelMsg.getChangeTime());
            } else {
                log.info("处理成功:{},{},{},{}", woLevelMsg.getWid(), woLevelMsg.getNewLevelId(), woLevelMsg.getOldLevelId(), woLevelMsg.getChangeTime());
                redisMqTemplate.sendMsg(entity);
            }
        }


    }
}
